Type Definitions

The following type definitions are available globally.

  • You may optionally set a receive filter for the socket.
  • A filter can provide several useful features:

  • 1. Many times udp packets need to be parsed.
  • Since

    Since the filter can run in its own independent queue, you can parallelize this parsing quite easily.

  • The end result is a parallel socket io, datagram parsing, and packet processing.
  • 2. Many times udp packets are discarded because they are duplicate/unneeded/unsolicited.
  • The filter can prevent such packets from arriving at the delegate.
  • And because the filter can run in its own independent queue, this doesn’t slow down the delegate.
  • - Since the udp protocol does not guarantee delivery, udp packets may be lost.
  • Many protocols built atop udp thus provide various resend/re-request algorithms.
  • This sometimes results in duplicate packets arriving.
  • A filter may allow you to architect the duplicate detection code to run in parallel to normal processing.

  • - Since the udp socket may be connectionless, its possible for unsolicited packets to arrive.
  • Such packets need to be ignored.
  • 3. Sometimes traffic shapers are needed to simulate real world environments.
  • A filter allows you to write custom code to simulate such environments.
  • The ability to code this yourself is especially helpful when your simulated environment
  • is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
  • or the system tools to handle this aren’t available (e.g. on a mobile device).
  • - parameter: data - The packet that was received.
  • - parameter: address - The address the data was received from.
  • See

    See utilities section for methods to extract info from address.

  • - parameter: context - Out parameter you may optionally set, which will then be passed to the delegate method.
  • For example, filter block can parse the data and then,
  • pass the parsed data to the delegate.
  • @returns - YES if the received packet should be passed onto the delegate.
  • NO if the received packet should be discarded, and not reported to the delegete.
  • Example:
  • GCDAsyncUdpSocketReceiveFilterBlock filter = ^BOOL (NSData *data, NSData *address, id *context) {
  • MyProtocolMessage *msg = [MyProtocol parseMessage:data];

  • *context = response;
  • return (response != nil);
  • };
  • [udpSocket setReceiveFilter:filter withQueue:myParsingQueue];
  • Declaration

    Objective-C

    typedef BOOL (^GCDAsyncUdpSocketReceiveFilterBlock)(NSData *, NSData *, id *)

    Swift

    typealias GCDAsyncUdpSocketReceiveFilterBlock = (Data?, Data?, AutoreleasingUnsafeMutablePointer

    Parameters

    data
    • The packet that was received.
    address
    • The address the data was received from. See utilities section for methods to extract info from address.
    context
    • Out parameter you may optionally set, which will then be passed to the delegate method. For example, filter block can parse the data and then, pass the parsed data to the delegate.
  • You may optionally set a send filter for the socket.
  • A filter can provide several interesting possibilities:
  • 1. Optional caching of resolved addresses for domain names.
  • The cache could later be consulted, resulting in fewer system calls to getaddrinfo.
  • 2. Reusable modules of code for bandwidth monitoring.
  • 3. Sometimes traffic shapers are needed to simulate real world environments.
  • A filter allows you to write custom code to simulate such environments.
  • The ability to code this yourself is especially helpful when your simulated environment
  • is more complicated than simple traffic shaping (e.g. simulating a cone port restricted router),
  • or the system tools to handle this aren’t available (e.g. on a mobile device).
  • - parameter: data - The packet that was received.
  • - parameter: address - The address the data was received from.
  • See

    See utilities section for methods to extract info from address.

  • - parameter: tag - The tag that was passed in the send method.
  • @returns - YES if the packet should actually be sent over the socket.
  • NO if the packet should be silently dropped (not sent over the socket).
  • Regardless of the return value, the delegate will be informed that the packet was successfully sent. *
  • Declaration

    Objective-C

    typedef BOOL (^GCDAsyncUdpSocketSendFilterBlock)(NSData *, NSData *, long)

    Swift

    typealias GCDAsyncUdpSocketSendFilterBlock = (Data?, Data?, Int) -> Bool

    Parameters

    data
    • The packet that was received.
    address
    • The address the data was received from. See utilities section for methods to extract info from address.
    tag
    • The tag that was passed in the send method.